package cn.xshi.sys.service.impl;
import java.util.*;
import cn.hutool.core.collection.CollectionUtil;
import cn.xshi.sys.model.SysPlatform;
import cn.xshi.sys.service.SysPlatformFeedbackService;
import cn.xshi.sys.service.SysPlatformService;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.sys.model.SysPlatformFeedback;
import org.springframework.stereotype.Service;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.dao.SysPlatformDao;

import javax.annotation.Resource;
/**
 * @Desc 平台信息发布
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class SysPlatformServiceImpl extends BaseService implements SysPlatformService {

	@Resource
	private SysPlatformDao sysPlatformDao;

	@Resource
	private SysPlatformFeedbackService sysPlatformFeedbackService;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<SysPlatform> getSysPlatformListByCondition(Map<String,Object> condition){
		try{
			return sysPlatformDao.getSysPlatformListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public SysPlatform getSysPlatformById(String id){
		try{
			SysPlatform sysPlatform = sysPlatformDao.getSysPlatformById(id);
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("platformId", id);
			List<SysPlatformFeedback> sysPlatformFeedback = sysPlatformFeedbackService.getSysPlatformFeedbackListByCondition(condition);
			sysPlatform.setSysPlatformFeedback(sysPlatformFeedback);
			return sysPlatform;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param sysPlatform
	* @return
	*/
	public int addSysPlatform(SysPlatform sysPlatform){
		int i = 0;
		try {
			Date date = getDate();
			sysPlatform.setCreateId(getCurrentAccountId());
			sysPlatform.setCreateTime(date);
			i = sysPlatformDao.addSysPlatform(sysPlatform);
			List<SysPlatformFeedback> sysPlatformFeedbackList = sysPlatform.getSysPlatformFeedback();
			if(CollectionUtil.isNotEmpty(sysPlatformFeedbackList)){
				for(SysPlatformFeedback platformFeedback: sysPlatformFeedbackList){
					platformFeedback.setCreateId(sysPlatform.getCreateId());
					platformFeedback.setCreateTime(date);
					platformFeedback.setPlatformId(sysPlatform.getId());
					platformFeedback.setId(toUUID());
				}
			}
			if(!sysPlatformFeedbackList.isEmpty()&& sysPlatformFeedbackList.size()>0){
				sysPlatformFeedbackService.addBatchSysPlatformFeedback(sysPlatformFeedbackList);
			}
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param sysPlatform
	* @return
	*/
	public int updateSysPlatform(SysPlatform sysPlatform){
		int i = 0;
		try {
			sysPlatform.setUpdateId(getCurrentAccountId());
			sysPlatform.setUpdateTime(getDate());
			i = sysPlatformDao.updateSysPlatform(sysPlatform);
			List<SysPlatformFeedback> sysPlatformFeedbackList = sysPlatform.getSysPlatformFeedback();
			List<SysPlatformFeedback> sysPlatformFeedbackAddList = new ArrayList<SysPlatformFeedback>();
			List<SysPlatformFeedback> sysPlatformFeedbackUpdateList = new ArrayList<SysPlatformFeedback>();
			if(CollectionUtil.isNotEmpty(sysPlatformFeedbackList)){
				for(int j = 0; j < sysPlatformFeedbackList.size(); j++){
					sysPlatformFeedbackList.get(j).setPlatformId(sysPlatform.getId());
					if(StringUtil.isEmpty(sysPlatformFeedbackList.get(j).getId())){
						sysPlatformFeedbackList.get(j).setId(toUUID());
						sysPlatformFeedbackList.get(j).setUpdateId(sysPlatform.getUpdateId());
						sysPlatformFeedbackList.get(j).setCreateTime(getDate());
						sysPlatformFeedbackAddList.add(sysPlatformFeedbackList.get(j));
					}else{
						sysPlatformFeedbackList.get(j).setUpdateTime(sysPlatform.getUpdateTime());
						sysPlatformFeedbackList.get(j).setUpdateId(sysPlatform.getUpdateId());
						sysPlatformFeedbackUpdateList.add(sysPlatformFeedbackList.get(j));
					}
				}
			}
			if(!sysPlatformFeedbackAddList.isEmpty()&& sysPlatformFeedbackAddList.size()>0){
				sysPlatformFeedbackService.addBatchSysPlatformFeedback(sysPlatformFeedbackAddList);
			}
			if(!sysPlatformFeedbackUpdateList.isEmpty()&& sysPlatformFeedbackUpdateList.size()>0){
				sysPlatformFeedbackService.updateBatchSysPlatformFeedback(sysPlatformFeedbackUpdateList);
			}
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delSysPlatform(Map<String,Object> condition){
		int i = 0;
		try {
			i = sysPlatformDao.delSysPlatform(condition);
			String[] platformIdList= (String[])condition.get("platformId");
			for(String platformId:platformIdList){
				sysPlatformFeedbackService.delSysPlatformFeedbackByForeignKey(platformId);
			}
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 批量添加
	* @param sysPlatformList
	* @return
	*/
	public int addBatchSysPlatform(List<SysPlatform> sysPlatformList){
		int i = 0;
		try {
			i = sysPlatformDao.addBatchSysPlatform(sysPlatformList);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 批量修改
	* @param sysPlatformList
	* @return
	*/
	public int updateBatchSysPlatform(List<SysPlatform> sysPlatformList){
		int i = 0;
		try {
			i = sysPlatformDao.updateBatchSysPlatform(sysPlatformList);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 批量修改（根据动态条件）
	* @param sysPlatformList
	* @return
	*/
	public int updateBatchSysPlatformBySelective(List<SysPlatform> sysPlatformList){
		int i = 0;
		try {
			i = sysPlatformDao.updateBatchSysPlatformBySelective(sysPlatformList);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}
}
